﻿using System;
using System.Collections;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.para;
using SharpSqlAnalyzer.pp.para.styleenums;
using SharpSqlAnalyzer.pp.stmtformatter;
using SharpSqlAnalyzer.pp.stmtformatter.type;
using SharpSqlAnalyzer.pp.utils;
using SharpSqlAnalyzer.stmt;

namespace SharpSqlAnalyzer.pp.processor.type.comm
{
	// Token: 0x02000255 RID: 597
	public class ExpressionProcessor : AbstractProcessor
	{
		// Token: 0x06001332 RID: 4914 RVA: 0x0031D6D4 File Offset: 0x0031B8D4
		public override void process(TParseTreeNode nodeArg)
		{
			TExpression texpression = nodeArg as TExpression;
			bool? isAndOrUnderWhere = new bool?(this.getParameter<bool>(typeof(bool)));
			if (isAndOrUnderWhere == null)
			{
				isAndOrUnderWhere = new bool?(false);
			}
			if (texpression != null)
			{
				if ((texpression.ExpressionType == EExpressionType.logical_and_t || texpression.ExpressionType == EExpressionType.logical_or_t) && texpression.getFlattedAndOrExprs().Count > TExpression.BigAndOrNestLevel)
				{
					ArrayList flattedAndOrExprs = texpression.getFlattedAndOrExprs();
					TExpression texpression2 = null;
					for (int i = 0; i < flattedAndOrExprs.Count; i++)
					{
						TExpression texpression3 = (TExpression)flattedAndOrExprs[i];
						if (texpression2 == null)
						{
							this.process(texpression3);
						}
						else
						{
							this.processOperator(texpression2, texpression3, isAndOrUnderWhere, i > 1);
							this.process(texpression3);
						}
						texpression2 = texpression3;
					}
					return;
				}
				if (texpression.LeftOperand != null && texpression.RightOperand != null)
				{
					this.process(texpression.LeftOperand);
					this.processOperator(texpression.LeftOperand, texpression.RightOperand, isAndOrUnderWhere);
					this.process(texpression.RightOperand);
					return;
				}
				if (texpression.LeftOperand != null && texpression.ExpressionType != EExpressionType.in_t && texpression.ExpressionType != EExpressionType.unary_minus_t && texpression.ExpressionType != EExpressionType.unary_plus_t)
				{
					this.processParenthesesNodeInExpression(texpression.LeftOperand, texpression.startToken, texpression.endToken);
					return;
				}
				if (texpression.RightOperand != null && texpression.ExpressionType != EExpressionType.in_t && texpression.ExpressionType != EExpressionType.unary_minus_t && texpression.ExpressionType != EExpressionType.unary_plus_t)
				{
					this.processParenthesesNodeInExpression(texpression.RightOperand, texpression.startToken, texpression.endToken);
					return;
				}
				if (texpression.FunctionCall != null)
				{
					this.processFunctionCall(texpression.FunctionCall);
					return;
				}
				this.processSingleNode(texpression);
			}
		}

		// Token: 0x06001333 RID: 4915 RVA: 0x0031D868 File Offset: 0x0031BA68
		private void processFunctionCall(TFunctionCall functionCall)
		{
			TSourceToken tsourceToken = SourceTokenSearcher.forwardSearch(functionCall.startToken, 5, "(");
			TSourceToken endToken = functionCall.endToken;
			if (tsourceToken != null && endToken != null)
			{
				SourceTokenOperator.removeWhitespaceAndReturnFromStart(this.Option, tsourceToken.container, tsourceToken.posinlist + 1);
				if (this.Option.wsPaddingParenthesesInFunctionCall)
				{
					SourceTokenOperator.addAfter(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(1));
				}
				SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, endToken);
				if (this.Option.wsPaddingParenthesesInFunctionCall)
				{
					SourceTokenOperator.addBefore(this.Option, endToken, SourceTokenOperator.createWhitespaceSourceToken(1));
				}
				if (functionCall.AgainstExpr != null)
				{
					this.process(functionCall.AgainstExpr);
				}
				if (functionCall.BetweenExpr != null)
				{
					this.process(functionCall.BetweenExpr);
				}
				if (functionCall.Expr1 != null)
				{
					this.process(functionCall.Expr1);
				}
				if (functionCall.Expr2 != null)
				{
					this.process(functionCall.Expr2);
				}
				if (functionCall.Expr3 != null)
				{
					this.process(functionCall.Expr3);
				}
				if (functionCall.Parameter != null)
				{
					this.process(functionCall.Parameter);
				}
				if (functionCall.InExpr != null)
				{
					TInExpr inExpr = functionCall.InExpr;
					if (inExpr.ExprList != null)
					{
						this.processFunctionCallParameters(inExpr.ExprList);
					}
					if (inExpr.Func_expr != null)
					{
						this.process(inExpr.Func_expr);
					}
					if (inExpr.GroupingExpressionItemList != null)
					{
						for (int i = 0; i < inExpr.GroupingExpressionItemList.size(); i++)
						{
							TGroupingExpressionItem groupingExpressionItem = inExpr.GroupingExpressionItemList.getGroupingExpressionItem(i);
							if (groupingExpressionItem.Expr != null)
							{
								this.process(groupingExpressionItem.Expr);
							}
							if (groupingExpressionItem.ExprList != null)
							{
								this.processFunctionCallParameters(groupingExpressionItem.ExprList);
							}
						}
					}
				}
				if (functionCall.RangeSize != null)
				{
					this.process(functionCall.RangeSize);
				}
				if (functionCall.XMLElementNameExpr != null)
				{
					this.process(functionCall.XMLElementNameExpr);
				}
				if (functionCall.XMLType_Instance != null)
				{
					this.process(functionCall.XMLType_Instance);
				}
				if (functionCall.XPath_String != null)
				{
					this.process(functionCall.XPath_String);
				}
				if (functionCall.Namespace_String != null)
				{
					this.process(functionCall.Namespace_String);
				}
				if (functionCall.Args != null)
				{
					this.processFunctionCallParameters(functionCall.Args);
				}
				if (functionCall.TrimArgument != null)
				{
					TTrimArgument trimArgument = functionCall.TrimArgument;
					TExpression texpression = trimArgument.StringExpression;
					if (texpression != null)
					{
						this.process(texpression);
					}
					texpression = trimArgument.TrimCharacter;
					if (texpression != null)
					{
						this.process(texpression);
					}
				}
				if (functionCall.OrderByList != null)
				{
					TOrderByItemList orderByList = functionCall.OrderByList;
					this.processOrderByList(orderByList);
				}
				if (functionCall.AnalyticFunction != null)
				{
					TExpressionList partitionBy_ExprList = functionCall.AnalyticFunction.PartitionBy_ExprList;
					if (partitionBy_ExprList != null)
					{
						this.processFunctionCallParameters(partitionBy_ExprList);
					}
					if (functionCall.AnalyticFunction.OrderBy != null)
					{
						TOrderByItemList items = functionCall.AnalyticFunction.OrderBy.Items;
						this.processOrderByList(items);
						return;
					}
				}
				else if (functionCall.WindowDef != null)
				{
					if (functionCall.WindowDef.PartitionClause != null)
					{
						TExpressionList expressionList = functionCall.WindowDef.PartitionClause.ExpressionList;
						if (expressionList != null)
						{
							this.processFunctionCallParameters(expressionList);
						}
					}
					if (functionCall.WindowDef.orderBy != null)
					{
						TOrderByItemList items2 = functionCall.WindowDef.orderBy.Items;
						this.processOrderByList(items2);
					}
				}
			}
		}

		// Token: 0x06001334 RID: 4916 RVA: 0x0031DB74 File Offset: 0x0031BD74
		private void processOrderByList(TOrderByItemList orderByList)
		{
			for (int i = 0; i < orderByList.size(); i++)
			{
				TExpression sortKey = orderByList.getOrderByItem(i).SortKey;
				if (sortKey != null)
				{
					this.process(sortKey);
				}
			}
		}

		// Token: 0x06001335 RID: 4917 RVA: 0x0031DBAC File Offset: 0x0031BDAC
		private void processFunctionCallParameters(TExpressionList columns)
		{
			if (columns == null || columns.size() == 0)
			{
				return;
			}
			TAlignStyle functionCallParametersStyle = this.Option.functionCallParametersStyle;
			TLinefeedsCommaOption functionCallParametersComma = this.Option.functionCallParametersComma;
			ColumnlistCommaProcessor.processColumns(this.Option, functionCallParametersComma, functionCallParametersStyle, columns);
		}

		// Token: 0x06001336 RID: 4918 RVA: 0x0031DBEC File Offset: 0x0031BDEC
		private void processSingleNode(TExpression node)
		{
			if (node.ExpressionType == EExpressionType.in_t)
			{
				if (node.SubQuery != null)
				{
					ExpressionProcessor.processParenthesesNodeInSubQuery(this.Option, node.SubQuery, node.startToken, node.endToken);
					return;
				}
			}
			else
			{
				if (node.CaseExpression != null)
				{
					this.processCase(node.CaseExpression);
					return;
				}
				if (node.SubQuery != null)
				{
					ExpressionProcessor.processParenthesesNodeInSubQuery(this.Option, node.SubQuery, node.startToken, node.endToken);
					return;
				}
				if (node.FunctionCall != null)
				{
					this.processParenthesesNodeInFunctionCall(this.Option, node.FunctionCall);
					return;
				}
				if (node.ExpressionType == EExpressionType.unary_minus_t || node.ExpressionType == EExpressionType.unary_plus_t)
				{
					this.processUnaryOperation(node);
				}
			}
		}

		// Token: 0x06001337 RID: 4919 RVA: 0x0031DC9D File Offset: 0x0031BE9D
		private void processUnaryOperation(TExpression node)
		{
			SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, node.startToken);
		}

		// Token: 0x06001338 RID: 4920 RVA: 0x0031DCB4 File Offset: 0x0031BEB4
		private void processParenthesesNodeInFunctionCall(GFmtOpt option, TFunctionCall fc)
		{
			TSourceToken tsourceToken = SourceTokenSearcher.forwardSearch(fc.FunctionName.startToken, 5, "(");
			TSourceToken endToken = fc.endToken;
			SourceTokenOperator.removeWhitespaceAndReturnFormBeforeAndAfter(this.Option, tsourceToken);
			SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, endToken);
			if (option.wsPaddingParenthesesInFunctionCall && endToken.posinlist - tsourceToken.posinlist > 1)
			{
				SourceTokenOperator.addAfter(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(1));
				SourceTokenOperator.addBefore(this.Option, endToken, SourceTokenOperator.createWhitespaceSourceToken(1));
			}
		}

		// Token: 0x06001339 RID: 4921 RVA: 0x0031DD34 File Offset: 0x0031BF34
		public static void processParenthesesNodeInSubQuery(GFmtOpt option, TCustomSqlStatement sql, TSourceToken leftParenthesis, TSourceToken rightParenthsis)
		{
			AbstractStmtFormatter abstractStmtFormatter = null;
			if (sql is TSelectSqlStatement)
			{
				abstractStmtFormatter = FormatterFactory.createSelectStmtFormatter(option);
			}
			else if (sql is TUpdateSqlStatement)
			{
				abstractStmtFormatter = FormatterFactory.createUpdateStmtFormatter(option);
			}
			else if (sql is TInsertSqlStatement)
			{
				abstractStmtFormatter = FormatterFactory.createInsertStmtFormatter(option);
			}
			else if (sql is TDeleteSqlStatement)
			{
				abstractStmtFormatter = FormatterFactory.createDeleteStmtFormatter(option);
			}
			if (abstractStmtFormatter == null)
			{
				return;
			}
			if (!"(".Equals(leftParenthesis.astext) || !")".Equals(rightParenthsis.astext))
			{
				abstractStmtFormatter.format(sql);
				return;
			}
			SourceTokenOperator.removeWhitespaceAndReturnFromStart(option, leftParenthesis.container, leftParenthesis.posinlist + 1);
			if (option.wsPaddingParenthesesOfSubQuery)
			{
				SourceTokenOperator.addAfter(option, leftParenthesis, SourceTokenOperator.createWhitespaceSourceToken(1));
			}
			abstractStmtFormatter.format(sql);
			SourceTokenOperator.removeWhitespaceAndReturnFromEnd(option, rightParenthsis.container, rightParenthsis.posinlist);
			if (option.wsPaddingParenthesesOfSubQuery)
			{
				SourceTokenOperator.addBefore(option, rightParenthsis, SourceTokenOperator.createWhitespaceSourceToken(1));
			}
		}

		// Token: 0x0600133A RID: 4922 RVA: 0x0031DE10 File Offset: 0x0031C010
		private void processParenthesesNodeInExpression(TExpression node, TSourceToken leftParenthesis, TSourceToken rightParenthsis)
		{
			if (node.ParentExpr != null && node.ParentExpr.ExpressionType == EExpressionType.parenthesis_t)
			{
				SourceTokenOperator.removeWhitespaceAndReturnFromStart(this.Option, leftParenthesis.container, leftParenthesis.posinlist + 1);
				if (this.Option.wsPaddingParenthesesInExpression)
				{
					SourceTokenOperator.addAfter(this.Option, leftParenthesis, SourceTokenOperator.createWhitespaceSourceToken(1));
				}
				this.process(node);
				SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, rightParenthsis.container, rightParenthsis.posinlist);
				if (this.Option.wsPaddingParenthesesInExpression)
				{
					SourceTokenOperator.addBefore(this.Option, rightParenthsis, SourceTokenOperator.createWhitespaceSourceToken(1));
					return;
				}
			}
			else
			{
				this.process(node);
			}
		}

		// Token: 0x0600133B RID: 4923 RVA: 0x0031DEB8 File Offset: 0x0031C0B8
		private void processCase(TCaseExpression caseExpr)
		{
			int num = SourceTokenOperator.curColumnNumberVT(caseExpr.startToken);
			TWhenClauseItemList whenClauseItemList = caseExpr.WhenClauseItemList;
			if (whenClauseItemList != null && whenClauseItemList.size() > 0)
			{
				for (int i = 0; i < whenClauseItemList.size(); i++)
				{
					TWhenClauseItem whenClauseItem = whenClauseItemList.getWhenClauseItem(i);
					TSourceToken startToken = whenClauseItem.startToken;
					SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, startToken);
					SourceTokenOperator.addBefore(this.Option, startToken, SourceTokenOperator.createReturnSourceToken());
					SourceTokenOperator.addBefore(this.Option, startToken, SourceTokenOperator.createWhitespaceSourceToken(num + this.Option.indentCaseFromSwitch));
					TSourceToken tsourceToken = SourceTokenSearcher.lastNotWhitespaceAndReturnToken(whenClauseItem.Return_expr.startToken.container, whenClauseItem.Return_expr.startToken.posinlist - 1);
					if (tsourceToken.astext.Equals("then", StringComparison.OrdinalIgnoreCase))
					{
						SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tsourceToken);
						if (this.Option.caseWhenThenInSameLine)
						{
							SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(1));
						}
						else
						{
							SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
							SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(num + this.Option.indentCaseFromSwitch + this.Option.indentCaseThen));
						}
					}
					this.process(whenClauseItem.Comparison_expr);
					this.process(whenClauseItem.Return_expr);
				}
			}
			if (caseExpr.Else_expr != null)
			{
				TSourceToken tsourceToken2 = SourceTokenSearcher.lastNotWhitespaceAndReturnToken(caseExpr.Else_expr.startToken.container, caseExpr.Else_expr.startToken.posinlist - 1);
				if (tsourceToken2.astext.Equals("else", StringComparison.OrdinalIgnoreCase))
				{
					SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tsourceToken2);
					SourceTokenOperator.addBefore(this.Option, tsourceToken2, SourceTokenOperator.createReturnSourceToken());
					SourceTokenOperator.addBefore(this.Option, tsourceToken2, SourceTokenOperator.createWhitespaceSourceToken(num + this.Option.indentCaseFromSwitch));
				}
				this.process(caseExpr.Else_expr);
			}
			TSourceToken endToken = caseExpr.endToken;
			SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, endToken);
			SourceTokenOperator.addBefore(this.Option, endToken, SourceTokenOperator.createReturnSourceToken());
			SourceTokenOperator.addBefore(this.Option, endToken, SourceTokenOperator.createWhitespaceSourceToken(num));
		}

		// Token: 0x0600133C RID: 4924 RVA: 0x0031E0E2 File Offset: 0x0031C2E2
		private void processOperator(TExpression left, TExpression right, bool? isAndOrUnderWhere)
		{
			this.processOperator(left, right, isAndOrUnderWhere, false);
		}

		// Token: 0x0600133D RID: 4925 RVA: 0x0031E0F0 File Offset: 0x0031C2F0
		private void processOperator(TExpression left, TExpression right, bool? isAndOrUnderWhere, bool isFlattedAndOr)
		{
			int num = SourceTokenOperator.curColumnNumberVT(left.getStartToken());
			if (isFlattedAndOr)
			{
				num = SourceTokenOperator.curColumnNumberVT(SourceTokenSearcher.lastNotWhitespaceAndReturnToken(left.getStartToken().container, left.getStartToken().posinlist - 1));
			}
			TSourceToken tsourceToken = SourceTokenSearcher.firstNotWhitespaceAndReturnToken(left.endToken.container, left.endToken.posinlist + 1, right.startToken.posinlist);
			if (ETokenType.ttconcatenationop.Equals(tsourceToken.tokentype))
			{
				SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
				SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(num + this.Option.indentLen));
				return;
			}
			if (!"and".Equals(tsourceToken.astext, StringComparison.CurrentCultureIgnoreCase) && !"or".Equals(tsourceToken.astext, StringComparison.CurrentCultureIgnoreCase))
			{
				if (tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals("+") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals("-") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals("*") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals("/") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals(">") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals(">=") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals("<") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals("<=") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals("==") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals("!=") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals("<>") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals("=") || tsourceToken.astext.TrimEnd(new char[1]).Trim().Equals("^="))
				{
					SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tsourceToken.container, tsourceToken.posinlist);
					SourceTokenOperator.removeWhitespaceAndReturnFromStart(this.Option, tsourceToken.container, tsourceToken.posinlist + 1);
					if (this.Option.wsPaddingOperatorArithmetic)
					{
						SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(1));
						SourceTokenOperator.addAfter(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(1));
					}
				}
				return;
			}
			if (left.ParentExpr != null && left.ParentExpr.ExpressionType == EExpressionType.between_t)
			{
				return;
			}
			SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tsourceToken);
			SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
			if (isAndOrUnderWhere.Value)
			{
				num -= tsourceToken.astext.Length + 1;
			}
			SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(num));
			SourceTokenOperator.removeWhitespaceAndReturnFromStart(this.Option, tsourceToken.container, tsourceToken.posinlist + 1);
			SourceTokenOperator.addAfter(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(1));
		}

		// Token: 0x0600133E RID: 4926 RVA: 0x0031E498 File Offset: 0x0031C698
		public static void processTypeName(GFmtOpt option, TTypeName type)
		{
			TSourceToken tsourceToken = SourceTokenSearcher.forwardSearch(type.startToken, 5, "(");
			if (tsourceToken != null)
			{
				TSourceToken tsourceToken2 = SourceTokenSearcher.forwardSearch(tsourceToken, 15, ")");
				if (tsourceToken2 != null)
				{
					SourceTokenOperator.removeWhitespaceAndReturnFromStart(option, tsourceToken.container, tsourceToken.posinlist + 1);
					if (option.wsPaddingParenthesesOfTypename)
					{
						SourceTokenOperator.addAfter(option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(1));
					}
					SourceTokenOperator.removeWhitespaceAndReturnFromEnd(option, tsourceToken2);
					if (option.wsPaddingParenthesesOfTypename)
					{
						SourceTokenOperator.addBefore(option, tsourceToken2, SourceTokenOperator.createWhitespaceSourceToken(1));
					}
				}
			}
		}
	}
}
